/*
 * PrintOrderGUI.java
 *
 * Created on April 2, 2007, 11:40 PM
 */

package edu.byu.isys413.jjenkins.view;

import edu.byu.isys413.jjenkins.data.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.NullCipher;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.sql.SQLException;
import javax.swing.JOptionPane;
 
/**
 * This form is created when the user clicks on the service type from the new order form
 * @author Gandolph
 */
public class PrintOrderGUI extends javax.swing.JFrame {
   PrintOrderLineRS print;
    //private List<List> printTypeList;
    /** Creates new form PrintOrderGUI */
    public PrintOrderGUI() {
        initComponents(); 
        pack();
    }
    
      /*This mehod ensures that only one instance of revenueSourceFactory
         *exists */
    private static PrintOrderGUI instance = null;
    
    /**
     * gets the single instance
     * @return 
     */
    public static synchronized PrintOrderGUI getInstance() {
        if (instance == null) {
            instance = new PrintOrderGUI();
        }
                instance.setLocationRelativeTo(null);
        instance.setVisible(true);
        return instance;

    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtExternal = new javax.swing.JTextField();
        txtNumber = new javax.swing.JTextField();
        setType = new javax.swing.JComboBox();
        txtQuantity = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        setSize = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        addPrintOrderToTx = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jLabel2.setText("Type:");

        jLabel3.setText("Number in set:");

        jLabel4.setText("Quantity:");

        jLabel6.setText("External ID:");

        setType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Glossy", "Matte" }));

        txtQuantity.setText("1");

        jLabel1.setText("Size:");

        setSize.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5x3", "6x4", "10x8" }));

        jLabel5.setText("Description:");

        addPrintOrderToTx.setText("Add Transaction Line");
        addPrintOrderToTx.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTL(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        jScrollPane1.setViewportView(txtDescription);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap(304, Short.MAX_VALUE)
                        .add(cancelButton)
                        .add(38, 38, 38)
                        .add(addPrintOrderToTx, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 153, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2Layout.createSequentialGroup()
                        .add(jLabel5)
                        .add(41, 41, 41)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel5)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(addPrintOrderToTx))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel3)
                            .add(jLabel6))
                        .add(29, 29, 29)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(txtExternal)
                            .add(txtNumber)
                            .add(setType, 0, 85, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 70, Short.MAX_VALUE)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(setSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 85, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(49, 49, 49)
                        .add(jLabel4)
                        .add(23, 23, 23)
                        .add(txtQuantity, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(19, 19, 19))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(setType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4)
                    .add(txtQuantity, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(setSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .add(17, 17, 17)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(txtNumber, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(22, 22, 22)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(txtExternal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
     * called from the new order form which also sets visible this form
     * @param print 
     */
     public void populate(PrintOrderLineRS print){
        
        this.print = print;
        this.setVisible(true);
        this.setTitle("Development Service");
        this.setLocationRelativeTo(null);
       
        
    }
     
    /**
     * rounds the decimals places to just two decimal places
     * @param d number to round
     * @param places what decimal place to round to
     * @return the rounded double
     */
    public static final double roundDouble(double d, int places) {
        return Math.round(d * Math.pow(10, (double) places)) / Math.pow(10,
            (double) places);
    }
    private void addTL(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTL
        System.out.println("Process had started");
        
        try{
               
        String size = (String)setSize.getSelectedItem ();
        String type = (String)setType.getSelectedItem ();
        int quantity = Integer.parseInt(txtQuantity.getText());
        String desc = txtDescription.getText ();
        int numInSet = Integer.parseInt(txtNumber.getText ());
        String extId = txtExternal.getText ();
        System.err.println("extId");
        //String custid = CustomerListGUI.getCustomersID ();
        
        PrintFormat pf = PrintFormatDAO.getInstance().getUnitPrice(type,size);
        
        double price = pf.getUnitPrice ();
        
        
        double totalPrice = roundDouble(price*numInSet*quantity, 2);
        JOptionPane.showMessageDialog(null, "The unit price for the selected combination is:" + price);
        JOptionPane.showMessageDialog(null, "Your Total Price without tax is:" + totalPrice+"\n"+"Thank You very much for shopping with us");
        
         this.dispose();
        
//        PhotoSet ps = PhotoSetDAO.getInstance().create(desc,numInSet,extId);
//        PhotoSetDAO.getInstance ().insertPhotoSet (ps);
//        //double price = p.getUnitPrice ();
//        
//        PrintOrderLineRS pline = (PrintOrderLineRS)PrintOrderLineRSDAO.getInstance().create ();
//        pline.setQuantity (quantity);
//        pline.setDescription (desc);
//        pline.setPrint_format_guid (pf.getId ());
//        pline.setPhoto_set_guid (ps.getId ());
//        pline.setPrice (roundDouble(totalPrice, 2));
//        pline.setType ("Print");
//        PrintOrderLineRSDAO.getInstance ().save (pline);
//        tx.addTxLine( (PrintOrderLineRS) pline, quantity);
        
        
//        //MembershipBO member = MembershipDAO.getInstance().getByCustID(custid);
        //String membID = member.getMemberID ();
//
//       UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
//        //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//       TransactionLineGUI frame = new TransactionLineGUI();
//       frame.addWindowListener(new WindowAdapter() {
//            public void windowClosing(WindowEvent evt) {
//              System.exit(0);
//            }
//       });
//        frame.pack();
//        frame.setVisible(true);
//        frame.setLocationRelativeTo(null);
//                   
//        //set the customer that is going to show there
//        frame.setTx(tx);
//        dispose();
        
        }catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_addTL
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PrintOrderGUI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addPrintOrderToTx;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox setSize;
    private javax.swing.JComboBox setType;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtExternal;
    private javax.swing.JTextField txtNumber;
    private javax.swing.JTextField txtQuantity;
    // End of variables declaration//GEN-END:variables
    
}
